class DoublyNode {
	int data;
	DoublyNode left;
	DoublyNode right;
	public DoublyNode(int d,DoublyNode n, DoublyNode n1){
		this.data = d;
		this.left = n;
		this.right = n1;
		}
}

public class DoubleLinkedList {

	DoublyNode head = null;
	DoublyNode tail = null;
	
	public void insertNode(int d){
		DoublyNode temp_head = head;
		DoublyNode n = new DoublyNode(d,null,null);
		if(temp_head == null) {
			head = n;
			tail = n;
		} else {
			while (temp_head.left!=null){
				temp_head = temp_head.left;
			}
			temp_head.left = n;
			n.right = temp_head;
			tail = n;
			return;
		}
	}
	
	public void printList(){
		DoublyNode temp_head = head;
		while(temp_head != null){
			System.out.print(temp_head.data+" -> ");
			temp_head = temp_head.left;
		}
	}
	
	public void reversePrintList(){
		DoublyNode temp_tail = tail;
		while(temp_tail != null){
			System.out.print(temp_tail.data+" -> ");
			temp_tail = temp_tail.right;
		}
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		DoubleLinkedList l = new DoubleLinkedList();
		l.insertNode(1);
		l.insertNode(2);
		l.insertNode(3);
		l.insertNode(4);
		l.insertNode(5);
		System.out.println("Linked list from head");
		l.printList();
		System.out.println("\nLinked list from tail");
		l.reversePrintList();

	}

}
